﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Windows.Controls;
using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using GameEngine;

namespace DaphneControls.ViewModel
{
    /// <summary>
    /// PDN Tag Add Form View Model
    /// </para>
    /// </summary>
    internal class PDNTagAddVM : ViewModelBase
    {
        /// <summary>
        /// new PDN tags
        /// </summary>
        private List<string> _lstPDNTags;
        private object _currentPDNTag = null;

        /// <summary>
        /// Filters out the new PDN tags
        /// </summary>
        public PDNTagAddVM(List<string> lstExistingTags, GameManager mng)
        {
            _lstPDNTags = mng.AllPDNTags.Where(s => lstExistingTags.Contains(s) == false).ToList();
            SelectCommand = new RelayCommand(new Action(this.Select), CanExecuteSelectCommand);
            CancelCommand = new RelayCommand(new Action(this.Cancel), () => { return true; });
        }

        /// <summary>
        /// Form X coordinate position
        /// </summary>
        public double PositionX
        { get; set; }

        /// <summary>
        /// Form Y coordinate position
        /// </summary>
        public double PositionY
        { get; set; }

        /// <summary>
        /// Select Command Property
        /// </summary>
        public RelayCommand SelectCommand
        { get; set; }

        /// <summary>
        /// Cancel Command Property
        /// </summary>
        public RelayCommand CancelCommand
        { get; set; }

        /// <summary>
        /// Method for evaluating, if select command can execute
        /// </summary>
        private bool CanExecuteSelectCommand()
        {
            return CurrentPDNTag != null;
        }

        /// <summary>
        /// Selected  PDN Tag
        /// </summary>
        public object CurrentPDNTag
        {
            get
            {
                return _currentPDNTag;
            }
            set
            {
                _currentPDNTag = value;
                SelectCommand.RaiseCanExecuteChanged();
            }
        }

        /// <summary>
        /// Returns the new PDN tags to the list
        /// </summary>
        public List<string> NewPDNTags
        {
            get
            {
                return _lstPDNTags;
            }
        }

        /// <summary>
        /// The selected PDN tag
        /// </summary>
        public string SelectedPDNTag
        { get; set; }

        /// <summary>
        /// Form result
        /// </summary>
        public bool SelectionDone
        { get; set; }


        /// <summary>
        /// Select button handler
        /// </summary>
        public void Select()
        {
            SelectedPDNTag = CurrentPDNTag as string;
            SelectionDone = true;
            NotifyWindowToClose();
        }

        /// <summary>
        /// Cancel button handler
        /// </summary>
        public void Cancel()
        {
            SelectionDone = false;
            NotifyWindowToClose();
        }

        /// <summary>
        /// Method sending the closing window message
        /// </summary>
        private void NotifyWindowToClose()
        {
            Messenger.Default.Send<NotificationMessage>(
                new NotificationMessage(this, "CloseWindow"));
        }
    }
}